﻿using System.Linq;
using ElectronicGovernment.Domain.Entities;
using ElectronicGovernment.Domain.Repositories;
using ElectronicGovernment.Infrastucture.EF;

namespace ElectronicGovernment.Infrastucture.Repositories
{
    public class GovernmentRepository : IGovernmentRepository
    {
        public Government Create(Government government)
        {
            using (var context = new DatabaseContext())
            {
                context.Governments.Add(government);
                context.SaveChanges();
            }

            return government;
        }

        public int CountGovernments()
        {
            using (var context = new DatabaseContext())
            {
                return context.Governments.Count();
            }
        }

        public Government GetGovernment(int id)
        {
            using (var context = new DatabaseContext())
            {
                return context.Governments.SingleOrDefault(g => g.Id == id);
            }
        }
    }
}